package cn.tedu.mall.seckill.config;

import cn.tedu.mall.seckill.timer.job.SeckillBloomInintialJob;
import cn.tedu.mall.seckill.timer.job.SeckillInitialJob;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration

public class SeckillQuartzConfiguration {

    //准备一个JobDetail封装SeckillInitialJob
    @Bean
    public JobDetail initSeckillJobDetail(){
        return JobBuilder.newJob(SeckillInitialJob.class)
                .withIdentity("initSeckillJobDetail")
                .storeDurably()
                .build();
    }

    //定义Trigger
    @Bean
    public Trigger initSeckillTrigger(){
        //每分钟执行一次
        CronScheduleBuilder cronSchedule = CronScheduleBuilder.cronSchedule("0 0/1 * * * ?");
        return TriggerBuilder.newTrigger()
                .forJob(initSeckillJobDetail())
                .withIdentity("initSeckillTrigger")
                .withSchedule(cronSchedule)
                .build();
    }

    //准备布隆过滤器的JobDetail
    @Bean
    public JobDetail initSeckillBloom(){
        return JobBuilder.newJob(SeckillBloomInintialJob.class)
                .withIdentity("bloom")
                .storeDurably()
                .build();
    }

    //定义Trigger
    @Bean
    public Trigger initSeckillBloomTrigger(){
        //每分钟执行一次
        CronScheduleBuilder cronSchedule = CronScheduleBuilder.cronSchedule("0 0/1 * * * ?");
        return TriggerBuilder.newTrigger()
                .forJob(initSeckillBloom())
                .withIdentity("initSeckillBloomTrigger")
                .withSchedule(cronSchedule)
                .build();
    }
}
